package com.vgemv.jsutilitysdkdemo.module.Request;

import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

import com.vgemv.jsutilitysdk.Request.JsRequestManager.JsRequestDictionaryCallback;
import com.vgemv.jsutilitysdk.Request.JsRequestManager2.JsRequestBaseData;
import com.vgemv.jsutilitysdk.Request.JsRequestManager2.JsRequestConfig;
import com.vgemv.jsutilitysdk.Request.JsRequestManager2.JsRequestInfo;
import com.vgemv.jsutilitysdk.Request.JsRequestManager2.JsRequestManager2;
import com.vgemv.jsutilitysdk.geometry.JsGeometryManager;
import com.vgemv.jsutilitysdk.log.JsLogManager;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemStyleDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuFragment.JsMenuFragment.JsMenuFragment;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewAdapter;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewBasicCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVmDefault;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Cookie;

public class RequestActivity extends Activity {


    public Handler mHandler = new Handler(Looper.getMainLooper());
    public List<Cookie> mCookies;
    public String mStr_host;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        JsRequestManager2.sharedInstance().serverAddress = "https://live.vgemv.com/api";

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setId(View.generateViewId());
        setContentView(linearLayout);


        JsMenuFragment menuFragment = new JsMenuFragment();
        menuFragment.moveToParentView(this,linearLayout);

        final int itemSize = JsGeometryManager.pixelFromDp(80);

        menuFragment.mBasicCallback = new JsMenuViewBasicCallback() {
            @Override
            public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                return null;
            }

            @Override
            public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

            }

            @Override
            public Rect jsMenuViewSetItemOffset(JsMenuView view, int index) {
                int spanCount = view.mMenuViewVm.mi_spanCount;
                int hPadding = (JsGeometryManager.getScreenSizeInPixel().getWidth() - spanCount * itemSize)/(spanCount + 2);
                int vPadding = JsGeometryManager.pixelFromDp(20);

                Rect rect = new Rect();
                if(index == 0) {
                    rect.top = vPadding;
                    rect.left = hPadding;
                    rect.right = hPadding;
                    rect.bottom = vPadding;
                }
                else if(index == view.vms().size() - 1) {
                    rect.top = vPadding;
                    rect.left = hPadding;
                    rect.right = hPadding;
                    rect.bottom = vPadding;
                }
                else {
                    rect.top = vPadding;
                    rect.left = hPadding;
                    rect.right = hPadding;
                    rect.bottom = vPadding;
                }
                return rect;

            }

            @Override
            public JsMenuViewVm jsMenuViewSetVm(JsMenuView f) {
                JsMenuViewVm vm = new JsMenuViewVm();
                vm.mi_spanCount = 4;
                return vm;
            }

            @Override
            public JsMenuItemView jsMenuViewCreateItemView(JsMenuView f, Context context) {
                return null;
            }

            @Override
            public void jsMenuViewAfterCreateView(JsMenuView fragment) {

            }

            @Override
            public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                ArrayList<JsMenuItemVm> models = new ArrayList<JsMenuItemVm>();

                JsMenuItemStyleDefault style = new JsMenuItemStyleDefault();
                style.mI_backgroundColor = Color.BLACK;
                style.mI_titleTextColor = Color.WHITE;
                style.mi_height = itemSize;
                style.mi_width = itemSize;

                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(0,"post请求-登录","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(1,"post请求-直播间列表","需要登录获得cookie",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(2,"清除cookie","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(3,"添加cookie","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(4,"请求用户资源","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(5,"Error","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(6,"Success","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(6,"ForceUndisplayReminder","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(7,"A long text","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(8,"A short text","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
                return models;
            }

            @Override
            public void jsMenuViewClickItem(JsMenuView fragmentAlpha, int i, JsMenuItemVm model) {
                switch (i) {
                    case 0:{
                        JsLogManager.logc_d("RequestActivity/jsMenuViewClickItem","%s","带Cookie的请求");
                        Map map_param = new HashMap<String,Object>();
                        map_param.put("userCode","huangwh");
                        map_param.put("userPasswd","A6E2A75B9D0902C342E4412BF6D94166");
                        map_param.put("pwdEncrypt","1");
                        map_param.put("appVersion","20206");
                        map_param.put("deviceCode","bb9a7b1e-d6d7-45f5-b8f1-da068c0a80ee");
                        map_param.put("hardwareID","");
                        map_param.put("imsi","");
                        map_param.put("mobileID","");
                        map_param.put("netType","");
                        map_param.put("os","ios");
                        map_param.put("validCode","");
                        JsRequestConfig config = new JsRequestConfig(JsRequestBaseData.JsRequestMethod.Post, JsRequestBaseData.JsRequestBodyInputType.MapToJson, JsRequestBaseData.JsResponseBodyOutputType.String);
                        config.interfacePath = "login.action";
                        config.requestBodyInput = map_param;
                        JsRequestManager2.sharedInstance().invokeRequest(config, new JsRequestBaseData.IJsResponseHandler() {
                            @Override
                            public void onResponse(int responseState, Object resultObject, String errMessage, JsRequestInfo info) {
                                Log.i("RequestActivity","xxx");
                            }
                        });
                        break;
                    }
                    case 1: {
                        onUiMyroomList();
                        break;
                    }
                    case 2: {
                        //清除cookie.
                        JsRequestManager2.sharedInstance().clearAllCookies();
                        break;
                    }
                    case 3: {
                        // 添加cookie.
//                        String cookiesJson = JsSharedPreferencesManager.get("cookies");
//                        Type type = new TypeToken<List<Cookie>>(){}.getType();
//                        List<Cookie> cookies = JsJsonManager.fromJson(cookiesJson,type);
//                        JsRequestManager.sharedInstance().setCookies(mStr_host,cookies);
                        break;
                    }
                    case 4: {
                        // 请求用户资源.
                        JsLogManager.logc_d("RequestActivity/jsMenuViewClickItem","%s","请求用户资源");
                        String url = "https://169.vgemv.com:59091/material_cms/api/ugc/material/list.action";
                        Map map_param = new HashMap<String,Object>();
                        map_param.put("state",1);
                        map_param.put("page",2);
                        map_param.put("size",12);
//                        JsRequestManager2.sharedInstance().postReqeustByJson(url, map_param,
//                                new JsRequestDictionaryCallback() {
//                                    @Override
//                                    public void onRequestResponse(int responseCode, Map resultObject, String detailInfo) {
//                                        JsLogManager.logc_d("RequestActivity",detailInfo);
//                                    }
//                                });
                    }
                    default:{
                        break;
                    }
                }
            }
        };
    }
    public void onUiMyroomList() {

        Map map_param = new HashMap<String,Object>();
        map_param.put("type","1");
        map_param.put("pagenum","0");
        map_param.put("pagesize",Integer.MAX_VALUE);
        JsRequestConfig config = new JsRequestConfig(JsRequestBaseData.JsRequestMethod.Post, JsRequestBaseData.JsRequestBodyInputType.MapToJson, JsRequestBaseData.JsResponseBodyOutputType.String);
        config.interfacePath = "liveRoom/listLiveRoom.action";
        config.requestBodyInput = map_param;
        JsRequestManager2.sharedInstance().invokeRequest(config, new JsRequestBaseData.IJsResponseHandler() {
            @Override
            public void onResponse(int responseState, Object resultObject, String errMessage, JsRequestInfo info) {
                String json = (String) resultObject;

            }
        });
    }
}